/*
 * Copyright (c) 2018, ZeeSec Technologies. All rights reserved.
 * ZeeSec PROPRIETARY/CONFIDENTIAL.
 */

package springtest.httpClient;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author ZhiQiang Chen
 * @date 2018-09-10 16:39:39
 */
@Controller
public class LoginController {

    @RequestMapping("/http/captcha")
    public void getCaptcha(HttpServletResponse response) throws Exception {
        OutputStream fos = response.getOutputStream();
        HttpClient client = HttpClientUtils.getHttpClient();
        HttpGet get = new HttpGet("https://sapling.vicp.net:44333/captcha.php");
        // get.setHeader("Content-type", "application/x-www-form-urlencoded");
        // get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2)
        // Gecko/20100101 Firefox/6.0.2");
        HttpResponse clientResponse = client.execute(get, HttpClientUtils.getContext());
        HttpClientUtils.printCookies();
        // 得到实体
        HttpEntity entity = clientResponse.getEntity();
        InputStream in = entity.getContent();
        response.setHeader("content-type", "image/jpg");
        response.setContentType("image/jpg");
        // 图片存入磁盘
        // FileOutputStream fos = new FileOutputStream("d:/mpl" +
        // UUID.randomUUID().toString() + ".jpg");
        byte[] buf = new byte[1024];
        int len = 0;
        while ((len = in.read(buf)) != -1) {
            fos.write(buf, 0, len);
        }
        fos.flush();
        fos.close();
    }

    @RequestMapping("/http/login")
    public void login(String captcha) throws Exception {
        HttpClient client = HttpClientUtils.getHttpClient();
        HttpPost post = new HttpPost("https://sapling.vicp.net:44333/captcha.php");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("user", "admin"));
        nvps.add(new BasicNameValuePair("pwd",
                "72334b0299bea38ff5750d9463edc3624d8411331ccd37e6303882662e73f2c1d9e6e250038673ea841a74ceb1e5710c4884ebbd682c2a847bbe371d305b3e80caa3899564372dd01d0463d596f9f65724ca87008ec0ad07eed727234fa69568a454fafd6b7e6814521dfe84a269db89aa0ab06e0744d97ba00e59aef8fb981cacdfb3d5f7e7553b28492c4d42519d29d455c4958ae1f1da7fc329415ff14c3655ba1fec21577dff688f1c27847d87a51483ace8190df20415c64a87b8a7e4ffe5ed676cd0d915f632b09ccc30eb64a433b07ba41beae0a755c15f0e900efd10db6867b603086eb8bb64465ab1d84f6570e76285338858846d6c6d54a7666f34"));
        nvps.add(new BasicNameValuePair("vericode", "captcha"));
        post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
        client.execute(post, HttpClientUtils.getContext());
    }

}
